import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

let sessionAdminInfo = sessionStorage.getItem('adminInfo')
const initAdminInfo = sessionAdminInfo ? JSON.parse(sessionAdminInfo) : {}

const state = {
	adminInfo: initAdminInfo,
}

const getters = {
	adminInfo: state => state.adminInfo
}
const mutations = {
	saveAdminInfo(state, adminInfo){
		state.adminInfo = adminInfo;
	},
	deleteAdminInfo(state) { // 清楚用户信息
		state.adminInfo = {}
	}
}

const actions = {
	async getAdminData({commit}){
		try{
			const res = await getAdminInfo()
			if (res.status == 1) {
				commit('saveAdminInfo', res.data);
			}else{
				throw new Error(res.type)
			}
		}catch(err){
			// console.log(err.message)
		}
	}
}

export default new Vuex.Store({
	state,
	actions,
	mutations,
	getters,
})
